﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_NewsList : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    NewsVo nv = new NewsVo();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup == "管理员")
            {
                dt = nd.getTopic();
                ddltopic.DataSource = dt;
                ddltopic.DataTextField = "Tname";
                ddltopic.DataValueField = "Tid";
                ddltopic.DataBind();
                bd();
            }
            else
            {
                Response.Redirect("../index.aspx");
            }
        }
    }

    public void bd()
    {
        if (ViewState["tid"] == null||ViewState["tid"] == "--请选择--")
        {
            ViewState["tid"] = "0";
        }
        if (ViewState["mid"] == null || ViewState["mid"] == "--请选择--")
        {
            ViewState["mid"] = "0";
        }
        if (ViewState["title"] == null || ViewState["title"] == "")
        {
            ViewState["title"] = "";
        }
        string tid = ViewState["tid"].ToString();
        string mid = ViewState["mid"].ToString();
        string title = ViewState["title"].ToString();
        dt = new DataTable();
        dt = nd.getNews(tid,mid,title);
        if (dt == null)
        {
            string script = "<script>window.alert('没有数据！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        if (dt.Rows.Count == 0)
        {
            string script = "<script>window.alert('没有数据！');</script>";
            Page.RegisterClientScriptBlock("", script);
            return;
        }
        gvnews.DataSource = dt;
        gvnews.DataBind();

        for (int i = 0; i < gvnews.Rows.Count; i++)
        {
            string id = dt.Rows[i][0].ToString();
            id = em.Encript(id);
            HyperLink hl = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hltitle");
            Label labtop = (Label)gvnews.Rows[i].Cells[0].FindControl("labtop");
            Label labtime = (Label)gvnews.Rows[i].Cells[0].FindControl("labtime");
            if (labtop.Text == "0")
            {
                labtop.Text = "";
            }
            else
            {
                labtop.Text = "是";
            }
            DateTime t = Convert.ToDateTime(labtime.Text);
            labtime.Text = t.ToShortDateString();
            //if (hl.Text.Length > 11)
            //{
            //    hl.Text = hl.Text.Substring(0, 11) + "...";
            //}
            hl.NavigateUrl = "News.aspx?nid=" + id;
            HyperLink hl2 = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hlupdate");
            hl2.NavigateUrl = "NewsUpdate.aspx?nid=" + id;
        }
    }


    protected void gvnews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvnews.PageIndex = e.NewPageIndex;
        if (ViewState["tid"] == null || ViewState["tid"] == "--请选择--")
        {
            ViewState["tid"] = "0";
        }
        if (ViewState["mid"] == null || ViewState["mid"] == "--请选择--")
        {
            ViewState["mid"] = "0";
        }
        if (ViewState["title"] == null || ViewState["title"] == "")
        {
            ViewState["title"] = "";
        }
        string tid = ViewState["tid"].ToString();
        string mid = ViewState["mid"].ToString();
        string title = ViewState["title"].ToString();
        dt = new DataTable();
        dt = nd.getNews(tid, mid, title);
        gvnews.DataSource = dt;
        gvnews.DataBind();

        for (int i = 0; i < gvnews.Rows.Count; i++)
        {
            int size = e.NewPageIndex * 20 + i;
            string id = dt.Rows[size][0].ToString();
            id = em.Encript(id);
            //id = id.Replace("+", "%2B");
            HyperLink hl = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hltitle");
            //if (hl.Text.Length > 11)
            //{
            //    hl.Text = hl.Text.Substring(0, 11) + "...";
            //}
            Label labtop = (Label)gvnews.Rows[i].Cells[0].FindControl("labtop");
            Label labtime = (Label)gvnews.Rows[i].Cells[0].FindControl("labtime");
            if (labtop.Text == "0")
            {
                labtop.Text = "";
            }
            else
            {
                labtop.Text = "是";
            }
            DateTime t = Convert.ToDateTime(labtime.Text);
            labtime.Text = t.ToShortDateString();
            hl.NavigateUrl = "News.aspx?nid=" + id;
            HyperLink hl2 = (HyperLink)gvnews.Rows[i].Cells[0].FindControl("hlupdate");
            hl2.NavigateUrl = "NewsUpdate.aspx?nid=" + id;
        }
    }


    protected void gvnews_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            nd.delNews(id);
        }
        bd();
    }

    protected void ddltopic_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlmodule.Items.Clear();
        ddlmodule.Items.Add("--请选择--");
        ViewState["tid"] = ddltopic.SelectedValue;
        ViewState["mid"] = "0";
        ViewState["title"] = "";
        txttitle.Text = "";
        dt = nd.getModule(ddltopic.SelectedValue);
        ddlmodule.DataSource = dt;
        ddlmodule.DataTextField = "Mname";
        ddlmodule.DataValueField = "Mid";
        ddlmodule.DataBind();
        bd();
    }
    protected void ddlmodule_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["mid"] = ddlmodule.SelectedValue;
        ViewState["title"] = "";
        txttitle.Text = "";
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        ViewState["title"] = txttitle.Text;
        bd();
    }
    protected void gvnews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[8].Attributes.Add("onclick", "return confirm('确认删除吗？')");
        }
    }
}
